package com.winona.ui;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.AppConfigFactory;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.domain.UserInfo;
import com.taplinker.core.rpc.Result;
import com.taplinker.core.rpc.command.SimpleBaseCommand;
import com.taplinker.core.rpc.http.HttpTemplate;
import com.taplinker.core.util.json.JsonUtil;
import com.winona.Base1Activity;
import com.winona.ConstantValue;
import com.winona.bean.Paper;

/**
 * 论文名称
 * 
 * @author djbian
 * 
 */
public class ThesisTitleActivity extends Base1Activity {
	private TextView title, thesis_name, content;
	private ImageView back, thesis_img;
	private Intent intent;

	private Paper paper;

	private List<Paper> list = null;
	private UserInfo user = AppConfigFactory.getInstance().getUserInfo();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.thesis_name);
		findView();
		setLinstener();
		getData();
	}

	private void getData() {

		new SimpleBaseCommand() {

			@Override
			public Result go() {

				return new HttpTemplate().exchange(ConstantValue.COMMON
						+ ConstantValue.THESISTITLE + "&id=" + user.getId());
			}

			@Override
			public void no(Result result) {

				super.no(result);
			}

			@Override
			public void yes(Result result) {
				String json = result.object.toString();
				if (StringUtils.isNotEmpty(json)) {
					try {
						org.json.JSONObject jo = new org.json.JSONObject(json);
						String jstr;
						jstr = jo.getString("body");
						// list = JsonUtil.jsonToList(jstr, Paper.class);
						paper = JsonUtil.jsonToObject(jstr, Paper.class);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

			}

		}.execute();

	}

	private void setLinstener() {
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				startActivity(PapersActivity.class);

			}
		});

	}

	private void findView() {
		title = (TextView) findViewById(R.id.thesis_title);
		back = (ImageView) findViewById(R.id.thesis_back);
		content = (TextView) findViewById(R.id.thesis_content);
		thesis_name = (TextView) findViewById(R.id.thesis_name);
		thesis_img = (ImageView) findViewById(R.id.thesis_img);

		HashMap<String, Object> map = (HashMap<String, Object>) getIntent()
				.getSerializableExtra("data");
		paper = (Paper) map.get("Paper_Thesis");

		title.setText(paper.getAcademicProject().getName());
		thesis_name.setText(paper.getAcademicProject().getContentName());
		// thesis_img.setImageURI(Uri.parse(ConstantValue.IMAGE
		// + paper.getAcademicProject().getImageUrl()));
		String url = paper.getAcademicProject().getImageUrl();
		ImageManager.getInstance().loadImage(ConstantValue.IMAGE + url,
				thesis_img, 60, 60, null);
		content.setText(paper.getAcademicProject().getContent());

	}
}
